#include "modbuswig.h"//首页
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(btnClickedTemSlot())); // 点击获取温度的信号槽
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(btnLedSlot())); // Led
    connect(ui->pushButton_fan, SIGNAL(clicked()), this, SLOT(btnFanSlot()));//风扇
    connect(ui->pushButton_buzzer, SIGNAL(clicked()), this, SLOT(btnBuzzerSlot()));

    movieFan=new QMovie(":/new/prefix1/fan (1).gif",QByteArray(),this);
    movieFan->start();
    ui->label_fan->setMovie(movieFan);

    movieLed =new QMovie(":/new/prefix1/led.gif",QByteArray(),this);
    movieLed->start();
    ui->label_led ->setMovie(movieLed);

    movieFire =new QMovie(":/new/prefix1/报警.gif",QByteArray(),this);
    movieFire->start();
    ui->label_fire->setMovie(movieFire);
}

// 用于接收MQTT的槽函数
void ModbusWig::dataUpSlot(QByteArray Array)
{
    QString payload=Array;
    // qDebug() << "首页" <<payload;
    QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
    QJsonDocument doc = QJsonDocument::fromJson(payload.toLatin1(), &jsonerror); // 将JSON数据解析为QJsonDocument对象
    if(!doc.isNull() && jsonerror.error == QJsonParseError::NoError)//检查是否解析成功
    {
        if(doc.isObject())  // 如果是一个对象
        {
            QJsonObject objectroot = doc.object();  // 获取根节点
            //qDebug() << "根节点：" << objectroot;
            QStringList listkeys = objectroot.keys();// 获取JSON对象的所有键
            //qDebug() << "所有键：" << listkeys;


            int type = objectroot.value("type").toInt();
            if(type!=4)
            {
                QJsonArray dataArray = objectroot.value("data").toArray(); // 获取 data 字段中的数组
                // 使用常量迭代器遍历 QJsonArray
                for (int i = 0; i < dataArray.size(); ++i)
                {
                    QJsonValue value = dataArray.at(i);
                    QJsonObject itemObj = value.toObject();
                    int key = itemObj.value("key").toInt();
                    double val = itemObj.value("val").toString().toDouble(); // 由于 val 是字符串，因此需要转换为 double
                    switch (key)
                    {
                    case 104:
                        ui->lineEdit_wd->setText(QString::number(val));
                        break;
                    case 105:
                        ui->lineEdit_sd->setText(QString::number(val));
                        break;
                    case 201:
                        ui->lineEdit_dy->setText(QString::number(val));
                        break;
                    case 202:
                        ui->lineEdit_dl->setText(QString::number(val));
                        break;
                    case 101:
                    {
                        QString numericValue = QString::number(val);
                        led = numericValue.toInt();
                        if(led==0)
                            ui->pushButton_2->setText("关");
                        else if(led==1)
                            ui->pushButton_2->setText("开");
                    }
                        break;
                    case 102:
                    {
                        QString numericValue = QString::number(val);
                        fan = numericValue.toInt();
                        if(fan==0)
                            ui->pushButton_fan->setText("关");
                        else if(fan==1)
                            ui->pushButton_fan->setText("开");
                    }
                        break;
                    case 103:
                    {
                        QString numericValue = QString::number(val);
                        buzzer = numericValue.toInt();
                        if(buzzer==0)
                            ui->pushButton_buzzer->setText("关");
                        else if(buzzer==1)
                            ui->pushButton_buzzer->setText("开");
                    }
                        break;

                    }
                }
            }
        }
    }
}
//点击刷新
void ModbusWig::btnClickedTemSlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("/app/data/down");
    QByteArray data = " { \" type \" :1,\" limit \" :\"part\"}";
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
//LED开关
void ModbusWig::btnLedSlot()
{
    QMQTT::Message message;
    QByteArray data;
    // 设置主题
    message.setTopic("/app/data/down");
    if(led==0)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 101, \" val \" : \" 1 \"}";
        led=1;
        ui->pushButton_2->setText("开");
        movieLed->start();
    }
    else if(led==1)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 101, \" val \" : \" 0 \"}";
        led=0;
        ui->pushButton_2->setText("关");
        movieLed->stop();
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }

}
//风扇开关
void ModbusWig::btnFanSlot()
{
    QMQTT::Message message;
    QByteArray data;
    // 设置主题
    message.setTopic("/app/data/down");
    if(fan==0)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 102, \" val \" : \" 1 \"}";
        fan=1;
        ui->pushButton_fan->setText("开");
        movieFan->start();
    }
    else if(fan==1)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 102, \" val \" : \" 0 \"}";
        fan=0;
        ui->pushButton_fan->setText("关");
        movieFan->stop();
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
//蜂鸣器开关
void ModbusWig::btnBuzzerSlot()
{
    QMQTT::Message message;
    QByteArray data;
    // 设置主题
    message.setTopic("/app/data/down");
    if(buzzer==0)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 103, \" val \" : \" 1 \"}";
        buzzer=1;
        ui->pushButton_buzzer->setText("开");
        movieFire->start();
    }
    else if(buzzer==1)
    {
        data = " { \" type \" :2,\" data \" : { \" key \" : 103, \" val \" : \" 0 \"}";
        buzzer=0;
        ui->pushButton_buzzer->setText("关");
        movieFire->stop();
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}

void ModbusWig::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QPixmap pic(":/new/prefix1/history.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pic);
}

ModbusWig::~ModbusWig()
{
    delete ui;
}
